//
//  ViewController.m
//  时钟实现
//
//  Created by hyl on 16/7/4.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic,strong) CALayer *secondLayer;
@property (nonatomic,strong) CALayer *minuteLayer;
@property (nonatomic,strong) CALayer *hourLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpMinuteLayer];
    [self setUpHourLayer];
    [self setUpSecodLayer];
    //定时器
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
-(void) timeChange{
    //获取当前日历对象
    NSCalendar *calendar=[NSCalendar currentCalendar];
    //获取日期的组件：年月日时分秒
    //components:需要获取的日期组件
    //formDate:获取哪个日期的组件
    NSDateComponents *cmp=[calendar components:NSCalendarUnitSecond |NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
    //获取秒
    NSInteger second=cmp.second;
    //获取分
    NSInteger minute=cmp.minute;
    //获取小时
    NSInteger hour=cmp.hour;
    //
//    NSLog(@"%li",(long)second);
    CGFloat secondAngle=(second*6)*(M_PI*2)/360;
    CGFloat minuteAngle=(minute*6)*(M_PI*2)/360;
    CGFloat hourAngle=(hour*30)*(M_PI*2)/360;
    self.secondLayer.transform=CATransform3DMakeRotation(secondAngle, 0, 0, 1);
    self.minuteLayer.transform=CATransform3DMakeRotation(minuteAngle, 0, 0, 1);
    self.hourLayer.transform=CATransform3DMakeRotation(hourAngle, 0, 0, 1);

}
#pragma mark - setUpSecodLayer
-(void) setUpSecodLayer{
    CALayer *layer=[CALayer layer];
    CGFloat clockW=self.imageView.bounds.size.width;
    layer.position=self.imageView.center;
    layer.anchorPoint=CGPointMake(0.5, 1);
    layer.bounds=CGRectMake(0, 0, 1,clockW*0.5-20);
    layer.backgroundColor=[UIColor redColor].CGColor;
    self.secondLayer=layer;
    [self.view.layer addSublayer:layer];
}
#pragma mark - setUpHourLayer
-(void) setUpHourLayer{
    CALayer *layer=[CALayer layer];
    CGFloat clockW=self.imageView.bounds.size.width;
    layer.position=self.imageView.center;
    layer.anchorPoint=CGPointMake(0.5, 1);
    layer.bounds=CGRectMake(0, 0, 2,clockW*0.5-35);
    layer.backgroundColor=[UIColor blackColor].CGColor;
    self.hourLayer=layer;
    [self.view.layer addSublayer:layer];
}
#pragma mark - setUpMinuteLayer
-(void) setUpMinuteLayer{
    CALayer *layer=[CALayer layer];
    CGFloat clockW=self.imageView.bounds.size.width;
    layer.position=self.imageView.center;
    layer.anchorPoint=CGPointMake(0.5, 1);
    layer.bounds=CGRectMake(0, 0, 2,clockW*0.5-25);
    layer.backgroundColor=[UIColor blackColor].CGColor;
    self.minuteLayer=layer;
    [self.view.layer addSublayer:layer];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
